package cn.com.open.itembank.controller;

import cn.com.open.itembank.dto.Category;
import cn.com.open.itembank.dto.ReturnModel;
import cn.com.open.itembank.entity.QuestionCategory;
import cn.com.open.itembank.service.QuestionCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lucky on 2017/9/4.
 */
@RestController
@RequestMapping("/questioncategory")
public class QuestionCategoryController {

    @Autowired
    private QuestionCategoryService service;

    /**
     * 根据试题应用类别id获取可用试题应用类别信息.
     *
     * @param id 试题应用类别id
     * @return 可用题库信息 question category info
     */
    @GetMapping(value = "/{id}")
    public QuestionCategory getQuestionCategoryInfo(@PathVariable(value = "id") String id) {
        return service.getQuestionCategoryInfo(id);
    }

    /**
     * 获取试题应用类别名称信息
     *
     * @param name 试题应用类别名称
     * @return the list
     */
    @GetMapping()
    public List<QuestionCategory> getList(@RequestParam(value = "name") String name) {
        return service.getList(name, false);
    }

    /**
     * 获取可用试题应用类别名称信息
     *
     * @param name 试题应用类别名称
     * @return the list
     */
    @GetMapping("/enabledlist")
    public List<QuestionCategory> getEnabledList(@RequestParam(value = "name") String name) {
        return service.getList(name, true);
    }

    /**
     * 根据试题应用类别名称串获取试题应用类别id串.
     *
     * @param names 试题应用类别名称串，逗号分割
     * @return the string
     */
    @GetMapping("/ids")
    public String getIdsFromNames(@RequestParam(value = "names") String names) {
        return service.getIdsFromNames(names);
    }

    /**
     * 新增试题应用类别.
     *
     * @param model the model
     * @return the int
     */
    @PostMapping
    public ReturnModel insert(@RequestBody Category model) {
        return service.insert(model);
    }

    /**
     * 修改试题应用类别.
     *
     * @param id   试题应用类别id
     * @param name 试题应用类别名称
     * @return the int
     */
    @PutMapping(value = "/{id}")
    public int update(@PathVariable(value = "id") String id, @RequestParam("name") String name) {
        return service.update(id, name);
    }

    /**
     * 更新试题应用类别状态.
     *
     * @param id 试题应用类别ID
     * @return the int
     */
    @PutMapping(value = "/{id}/status")
    public int changeStatus(@PathVariable(value = "id") String id) {
        return service.changeStatus(id);
    }

    /**
     * 删除试题应用类别
     *
     * @param id 试题应用类别id
     * @return the int
     */
    @DeleteMapping(value = "/{id}")
    public int delete(@PathVariable(value = "id") String id) {
        return service.delete(id);
    }
}
